package com.Food;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import android.util.Log;

public class DBConnection
{
    private static String driver = "net.sourceforge.jtds.jdbc.Driver";
	private static final String url = "jdbc:jtds:sqlserver://10.34.1.12:1433;databaseName=Wcf";
    private static String userName = "Michael";
    private static String password = "123";
    
	private static final String TAG = null;

    public DBConnection()
    {
    	
    }
    public void insert(String query)
    {
    	try
    	{
	    	Log.v(TAG, query);
			Class.forName(driver).newInstance();
			Connection conn = DriverManager.getConnection(url, userName, password);
			Statement statement = conn.createStatement();
			statement.execute(query);
    	}
    	catch(Exception e)
    	{
    		Log.v(TAG, e.toString());
    	}
    }
    
	public String Query(String query)
	{
		ResultSet results = null;
		
		try
		{
			Log.v(TAG, query);
			Class.forName(driver).newInstance();
			Connection conn = DriverManager.getConnection(url, userName, password);
			Statement statement = conn.createStatement();
			results = statement.executeQuery(query);
			
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		try 
		{
			if( results.next() )
			{
				Log.v(TAG, "Found something");
				int i = results.getInt(1);
				Integer in = i;
				String s = in.toString();
				return s;	                            
			}
			else
			{
				Log.v(TAG, "Found nothing");
			}
			
		} 
		catch (SQLException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "error";
	}
	
	public byte[] getPic(String query)
	{
		ResultSet results = null;
		
		try
		{
			Log.v(TAG, query);
			Class.forName(driver).newInstance();
			Connection conn = DriverManager.getConnection(url, userName, password);
			Statement statement = conn.createStatement();
			results = statement.executeQuery(query);
			
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		try 
		{
			if( results.next() )
			{
				Log.v(TAG, "Found something");
				return results.getBytes(1);                            
			}
			else
			{
				Log.v(TAG, "Found nothing");
			}
			
		} 
		catch (SQLException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
/*    public static void closeConnection()
    {
       	try
       	{
            con.close();
            System.out.println("The connection is closed");
        }
       	catch (Exception e)
       	{
       		System.out.println("Error trying to close the database " +  e.getMessage());
       	}
    }
    
    public Connection getDBcon()
    {
       return con;
    }*/
}